$(function () {
    allClassTable();
    getGradeList();
})


function query(params) {
    var type = $("#gradeSelect").val();
    var param = {
        type:type,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

function allClassTable() {
    $("#class_list").bootstrapTable({
        url:$.ip+"/classInfo/getClassListByUserInfo?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '年级',
                align: 'center',
                field: 'gradeName',
                valign: 'middle',

            }  ,
            {
                title: '班级',
                align: 'center',
                field: 'className',
                valign: 'middle',

            },
            {
                title: '班级人数',
                align: 'center',
                field: 'studentSum',
                valign: 'middle',

            },
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="remove(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a onclick="update(this)" data-id="'+id+'" class="btn btn-warning " href="javascript:;">编辑</a>';

                }
            }
        ]
    });
}

function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
        if(data.success)
        {
            $.each(data.obj.list,function (i,d) {
                $("#gradeSelect").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                $("#gradeSelect1").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
            });
        }
        else{
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("初始化数据失败");
    });
}



function add(){
    $("#className").val("");
    $("#gradeSelect1").val("");
    popdiv("ban-info","添加",["50%","40%"],addClass)
}

function addClass() {
    var className = $("#className").val();
    var gradeId = $("#gradeSelect1").val();
    $.DemoPost("/classInfo/add?token="+sessionStorage.getItem("token"),
        {"className":className,"gradeId":gradeId},
        function (data) {
            layer.close(layer.index);
            layer.msg("添加成功");
            $("#class_list").bootstrapTable('refresh');
        },function (error) {
            layer.msg("失败");
        })

}


function remove(obj) {
    var id = $(obj).data("id");
    $.DemoGet("/classInfo/delete/"+id,function (data) {
        if(data.success)
        {
            layer.msg(data.msg);
            $("#class_list").bootstrapTable('refresh');
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
}


function update(obj){
    var id = $(obj).data("id");
    $("#classId").val(id);
    $.DemoGet("/classInfo/info/"+id,function (data) {
        if(data.success)
        {
            $("#gradeSelect1").val(data.obj.gradeId);
            $("#className").val(data.obj.className);
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
    popdiv("ban-info","修改",["50%","40%"],updateClass)
}

function updateClass() {
    var id = $("#classId").val();
    var className = $("#className").val();
    var gradeId = $("#gradeSelect1").val();
    $.DemoPost("/classInfo/update",{"id":id,"className":className,"gradeId":gradeId},function (data) {
        if(data.success)
        {
            layer.close(layer.index);
            layer.msg(data.msg);
            $("#class_list").bootstrapTable('refresh');
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
}

$("#btnQuery").click(function () {
    $("#class_list").bootstrapTable('refresh');

});
